# # Need to define new postprocessing function
# setwd("/Users/musashi/Princeton Dropbox/Musashi Hinck/dsl-image/dsl-image-simulation")
# design_grid <- expand.grid(
#     n_label = c(100, 250, 500, 750, 1000, 2000),
#     gs_acc = c(0.5, 0.75, 0.9, 0.95, 0.99, 1.0),
#     q_acc = c(0.75, 0.8, 0.85)
# ) # 108 designs
# results_dir <- paste0("results/sim2/")
# sim_result <- readRDS(paste0(results_dir, "001.rds"))

# true_coef <- sim_result$true_coef
# results <- sim_result$sim_results

# # Check if confint calculation is correct
# res <- results[[1]]
# res$dsl_confint == cbind(res$dsl_coef - 1.96*res$dsl_se, res$dsl_coef + 1.96*res$dsl_se)

# # From simulation_utils.R
# single_coverage <- function(coefs, confints){
#   return((coefs >= confints[,1]) & (coefs <= confints[,2])) 
# }

# # 


# Main script
extract_simulation <- function(design_grid, results_dir){
  # Extract results from a single simulation
  extract_result <- function(i){
    results_file <- paste0(results_dir, sprintf("%03d", i), ".rds")
    if(!file.exists(results_file)){
      return(rep(NA, 6))
    }
    sim_result <- readRDS(results_file)
    sim_result <- sapply(
      c("so_bias", "dsl_bias", "so_rmse", "dsl_rmse", "so_coverage", "dsl_coverage"),
      function(x) sim_result[[x]]
    ) 
    return(sim_result)
  }

  # Collect into dataframe
  results <- lapply(1:nrow(design_grid), extract_result)
  results <- do.call(rbind, results) |> apply(2, unlist) |> as.data.frame()
  out_df <- cbind(design_grid, results)
  return(out_df)
}

main <- function(){
  ## Simulation 1
  design_grid <- expand.grid(
      n_label = c(100, 250, 500, 750, 1000, 2000),
      gs_acc = c(1.0),
      q_acc = c(0.5, 0.75, 0.9, 0.95, 0.99, 1.0)
  )
  results_dir <- paste0("results/sim1/")
  results_df <- extract_simulation(design_grid, results_dir)
  write.csv(results_df, paste0("results/final/sim1.csv"))

  ## Simulation 2
    design_grid <- expand.grid(
        n_label = c(100, 250, 500, 750, 1000, 2000),
        gs_acc = c(0.75, 0.8, 0.9, 0.95, 0.99, 1.0),
        q_acc = c(0.75)
    ) # 36 designs
  results_dir <- paste0("results/sim2/")
  results_df <- extract_simulation(design_grid, results_dir)
  write.csv(results_df, paste0("results/final/sim2.csv"))
}

main()